#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
int size;
typedef struct SeqList {
	struct SeqList* pt;
	int data;
}SeqList;
void ListInitiate(){
	size = 0;
};
int ListLength()
{
	return size;
}
SeqList* ListInsert(SeqList* L, int i, DataType x)
{
	if (i < 0 || i > size) {
		printf("参数i不合法! \n");
		return L;
	}
	else {
		SeqList* newdata = (SeqList*)malloc(sizeof(SeqList));
		SeqList* temps = L;
		if (i == 0) {
			L = newdata;
			L->data = x;
			L->pt = temps;
		}
		else {
			SeqList* temp = L;
			for (int a = i; a > 0; a--) {
				temps = temp;
				temp = temp->pt;
			}
			temps->pt = newdata;
			newdata->data = x;
			newdata->pt = temp;
		}
		size++;/*元素个数加1*/
		return L;
	}
}
SeqList* ListDelete(SeqList* L, int i, DataType* x)
{
	if (size <= 0) {
		printf("顺序表已空无数据元素可删! \n");
	}
	else if (i < 0 || i > size - 1)
	{
		printf("参数i不合法");
	}
	else
	{
		SeqList* temp = L;
		SeqList* temps = L;
		for (int a = 0; a < i; a++) {
			temps = temp;
			temp = temp->pt;
		}
		temps->pt = temp->pt;
		temps = temps->pt;
		*x = temp->data;
		free(temp);
		size--;/*数据元素个数减1*/
		if (i == 0) {
			return temps;
		}
		return L;
	}
}
int ListGet(SeqList* L, int i, DataType* x)
{
	if (i < 0 || i > size - 1) {
		printf("参数i不合法! \n");
		return 0;
	}
	else {
		for (int a = 0; a < i; a++) {
			L = L->pt;
		}
		*x = L->data;
		return 1;
	}
}

int ListSearch(SeqList* L, DataType x) {
	int a = 0;
	if (L->data == x) {
		return 0;
	}
	while (a < size) {
		if (L->data == x) {
			return a;
		}
		L = L->pt;
		a++;
	}
	return -1;
}

int main(int argc, char* argv[])
{
	SeqList* myList = 0;
	int i, x;
	ListInitiate(myList);
	for (i = 0; i < 10; i++)
		myList = ListInsert(myList, i, i + 1);
	myList = ListDelete(myList, 5, &x);
	printf("插入10个元素,删除第下标为5的元素后线性表:");
	for (i = 0; i < ListLength(); i++)
	{
		ListGet(myList, i, &x);
		printf("%d ", x);
	}
	printf("\n数据9的索引值:%d\n数据11的索引值:%d", ListSearch(myList, 9), ListSearch(myList, 11));
	getchar();
	return 0;
}